package com.c.ctgproject.ui.production

import android.app.Activity
import android.content.Intent
import android.util.Log
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.MovingProductionEntity
import com.c.ctgproject.ui.production.doing.DoingProductionActivity
import com.c.ctgproject.ui.production.material.MaterialWeightActivity
import com.c.ctgproject.ui.production.pre.PreProductionActivity
import com.c.ctgproject.utils.CustomToast
import com.c.ctgproject.utils.*
import com.c.ctgproject.utils.showPopupWindow
import com.yzq.zxinglibrary.android.CaptureActivity
import com.yzq.zxinglibrary.common.Constant
import kotlinx.android.synthetic.main.activity_newvehicles.*
import kotlinx.android.synthetic.main.activity_production.*
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import org.jetbrains.anko.startActivity
import org.json.JSONObject
import java.io.File

/**
 *作者:LiBW
 *创建日期: 2020/9/1
 *描述: 生产部门 页面
 */
class ProductionActivity : BaseActivity() {
    private val mViewModel: ProductionViewModel by lazy { ViewModelProvider(this)[ProductionViewModel::class.java] }

    //二维码回调
    private val requestCodeScan = 10001
    override fun getLayout() = R.layout.activity_production
    override fun event() {
        changeTransparentStatusBar()
        //车辆是否可拆回调
        mViewModel.movingProduction.observe(this, Observer {
            loading.dismiss()
            val response = JSONObject(it)
            if (it == null) return@Observer
            if (response.getString("status") == "0") {
                val result = response.getJSONObject("data")
                val isOperatingCar = result.getString("isOperatingCar")
                val carStatus = result.getString("carStatus")
                //如果是营运车辆并且已经二验
                if (isOperatingCar == "1" && carStatus == "6") {
                    showToast("该车已经拆解完成")
                    return@Observer
                }

                //1为营运车辆 可以直接进行拆解
                if (isOperatingCar == "1" && carStatus == "9") {
                    //进入拆解中
                    startActivity<DoingProductionActivity>("Message" to result.toString())
                    return@Observer
                }

                if (isOperatingCar == "1" && carStatus != "9" && carStatus != "10") {
                    startActivity<PreProductionActivity>("Message" to result.toString())
                    return@Observer
                }



                if (carStatus.isNotEmpty() && carStatus == "10") {
                    showToast(response.getString("msg"))
                    return@Observer
                }
                //1 车辆仓库 2拆解车间 3物料仓库
                if (carStatus.isNotEmpty() && carStatus == "9") {
                    //进入拆解中
                    startActivity<DoingProductionActivity>("Message" to result.toString())
                } else {
                    //不为9进入拆解前
                    startActivity<PreProductionActivity>("Message" to result.toString())
                }
            } else {
                CustomToast.showToast(this, response.getString("msg"))
                MyLog.d(response.getString("msg"))
            }
        })
        //错误通用回调
        mViewModel.error.observe(this, Observer {
            loading.dismiss()
            CustomToast.showToast(this, it)
        })
    }

    override fun click() {
        //扫码按钮
        mScanQrBTN.setOnClickListener {
            startActivityForResult(Intent(this, CaptureActivity::class.java), requestCodeScan)
        }
        //物料称重
        mMaterialWeightBTN.setOnClickListener {
            startActivity<MaterialWeightActivity>()
        }
        mSettingIV.setOnClickListener {
            showPopupWindow(it)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            //二维码回调
            if (requestCode == requestCodeScan && data != null) {
                val content: String? = data.getStringExtra(Constant.CODED_CONTENT)
                MyLog.d("二维码扫描结果:$content")
                if (content != null) {
                    val messages: List<String> = content.split(":")
                    if (messages.size <= 1) {
                        CustomToast.showToast(this, "二维码不包含库位信息")
                    }
                    if (messages[0] == "carCode") {
                        loading.show()
                        mViewModel.movingProduction(messages[1])
                    } else {
                        CustomToast.showToast(this, "请扫拓号单二维码")
                    }
                }
            }
        }
    }

    override fun onBackPressed() {
        exitApp()
    }
}